package weaver.aiyh_pcn.copy_attachment;

import aiyh.utils.Util;
import aiyh.utils.mapUtil.ParaMap;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.copy_attachment.dao.ConfigTableData;
import com.api.aiyh_pcn.copy_attachment.model.ConfigEmpty;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/10/9 0009 13:42
 * 模板覆盖文件
 */


public class CopyAttachmentAction extends ToolUtil implements Action {
	//当前类名称
	private final String className = "AddCustomer_Action";
	private String overrideField;


	@Override
	public String execute(RequestInfo requestInfo) {
		this.writeDebuggerLog(className + "======>" +  overrideField);
		try {
			String workflowid = requestInfo.getWorkflowid();
			//流程请求ID
			String requestid = Util.null2String(requestInfo.getRequestid());
			//流程表单名称
			String tableName = requestInfo.getRequestManager().getBillTableName();
			ConfigEmpty config = ConfigTableData.getConfig(workflowid);
//		查询是流程数据
			String query = "select * from " + tableName + " where requestid = ?";
			RecordSet rs = new RecordSet();
			rs.executeQuery(query,requestid);
			Map<String, Object> requestMap = Util.recordSet2Map(rs);
			if(requestMap == null || config == null){
				return Action.SUCCESS;
			}
			if(!requestMap.get(config.getShowField()).equals(config.getShowValue())){
				return Action.SUCCESS;
			}
//		需要模板覆盖
			String attachmentValue = String.valueOf(requestMap.get(config.getAttachmentField()));
			String overrideValue = String.valueOf(requestMap.get(overrideField));
			this.writeDebuggerLog(requestMap.toString());
//		查询这些文件的相关信息
			query = "select docid,imagefilename from docimagefile where docid in (" + attachmentValue + ")";
			this.writeDebuggerLog(query);
			rs.executeQuery(query);
			List<AttachmentIdName> attachmentList = Util.recordeSet2Array(rs, AttachmentIdName.class);
			query = "select docid,imagefilename from docimagefile where docid in (" + overrideValue + ")";
			rs.executeQuery(query);
			this.writeDebuggerLog(query);
			List<AttachmentIdName> overrideList = Util.recordeSet2Array(rs, AttachmentIdName.class);
			this.writeDebuggerLog(attachmentList.toString());
			this.writeDebuggerLog(overrideList.toString());
			if (attachmentList.size() != overrideList.size()){
				requestInfo.getRequestManager().setMessagecontent("合同模板与上传模板数量不一致");
				requestInfo.getRequestManager().setMessageid("合同模板与上传模板数量不一致");
				return Action.FAILURE_AND_CONTINUE;
			}
//		需要移除的文件
			List<AttachmentIdName> removeFile = new ArrayList<>();
//		移除所有的pdf文件
			for (AttachmentIdName overrideIdName : overrideList) {
				String fileName = overrideIdName.getImagefilename();
				String extension = "";
				this.writeDebuggerLog(overrideIdName.toString());
				int index = fileName.lastIndexOf('.');
				if (index > 0) {
					extension = fileName.substring(index + 1);
				}
				this.writeDebuggerLog(extension);
				if("pdf".equalsIgnoreCase(extension)){
					removeFile.add(overrideIdName);
				}
			}
//			删除文件
			for (AttachmentIdName overrideIdName : removeFile) {
				overrideList.remove(overrideIdName);
			}
//		添加pdf文件
			for (AttachmentIdName attachmentIdName : attachmentList) {
				String fileName = attachmentIdName.getImagefilename();
				String extension = "";
				this.writeDebuggerLog(attachmentIdName.toString());
				int index = fileName.lastIndexOf('.');
				if (index > 0) {
					extension = fileName.substring(index + 1);
				}
				this.writeDebuggerLog(extension);
				if("pdf".equalsIgnoreCase(extension)){
					overrideList.add(attachmentIdName);
				}
			}
			if (attachmentList.size() != overrideList.size()){
				requestInfo.getRequestManager().setMessagecontent("合同模板与上传模板数量不一致");
				requestInfo.getRequestManager().setMessageid("合同模板与上传模板数量不一致");
				return Action.FAILURE_AND_CONTINUE;
			}
			StringBuilder overrideBuilder = new StringBuilder();
			for (AttachmentIdName overrideIdName : overrideList) {
				overrideBuilder.append(",").append(overrideIdName.getDocid());
			}
			String overrideResult = Util.removeSeparator(overrideBuilder);
			this.writeDebuggerLog(overrideResult);
			PrepSqlResultImpl sqlResult = Util.createSqlBuilder().updateSql(tableName, ParaMap.create().put(this.overrideField, overrideResult),
					Util.createPrepWhereImpl().whereAnd("requestid").whereEqual(requestid));
			rs.executeUpdate(sqlResult.getSqlStr(),sqlResult.getArgs());
		}catch (Exception e){
			this.writeErrorLog(e.getMessage());
		}
		return Action.SUCCESS;
	}

	public String getOverrideField() {
		return overrideField;
	}

	public void setOverrideField(String overrideField) {
		this.overrideField = overrideField;
	}
}
